function checkVersion() {
	plus.nativeUI.showWaiting("正在检测更新...");
	plus.runtime.getProperty(plus.runtime.appid, function(info) {
		checkUpdate(info.versionCode);
	});
}

function closeSplash() {
	plus.navigator.closeSplashscreen();
}

// 检测更新
function checkUpdate(version) {
	setTimeout(() => {
		makeFetch(updateServer + '/check', JSON.stringify({
			"clientVersion": version
		}), (data) => {
			if (JSON.parse(data).ok) {
				downWgt(decodeBase64(JSON.parse(data).data));
			}
		}, (error) => {});
	}, 1500);
}

// 下载wgt文件
function downWgt(fileName) {
	setTimeout(() => {
		plus.nativeUI.showWaiting("正在下载更新...");
		plus.downloader.createDownload(updateServer + "/hotkit/" + fileName, {
			filename: "_doc/update/"
		}, function(d, status) {
			console.log(JSON.stringify(d));
			removeRemote();
			if (status === 200) {
				installWgt(d.filename);
			} else {
				plus.nativeUI.alert("下载更新资源失败");
			}
		}).start();
	}, 1500);
}

// 更新应用资源  
function installWgt(path) {
	setTimeout(() => {
		plus.nativeUI.showWaiting("正在安装更新...");
		plus.runtime.install(path, {
			force: true
		}, function() {
			setTimeout(() => {
				plus.nativeUI.closeWaiting();
				plus.nativeUI.confirm("新版本已更新完成, 请重启后生效. 若未生效建议清理应用后台并重新打开.", function(e) {
					if(e.index === 1) {
						return;
					}
					plus.runtime.restart();
				}, {
					"title": "完成提醒",
					"buttons": ["重启", "稍后"],
					"verticalAlign": "bottom",
				});
			}, 1500)
		}, function(e) {
			plus.nativeUI.alert("更新失败: " + e.message);
		});
	}, 1500);
}

function removeRemote(fileName) {
	makeFetch(updateServer + "/rmRemote", JSON.stringify({}), (data) => {}, (error) => {});
}
